package ast.nodes;

import scanner.Token;
import scanner.TokenType;
import ast.ASTNode;
import ast.Evaluation;
/**
 * The first child of a ForNode is a ParamNode with 'int', the subsequent children are Series Nodes
 * which are run from
 */
public class ForNode extends CommandNode {
    public ForNode(Token data) {
        super(data);
    }
    
    public ForNode() {
        this(new Token("for", TokenType.FOR));
    }

    @Override
    public String toString(int depth) {
        String seriesStr = "";
        for (ASTNode child : getChildren()) {
            seriesStr += child.toString(depth+1);
        }

        return indent(depth) + this.data.getTokenString() + ":" + seriesStr;
    }

    @Override
    public void run(Evaluation input) throws Exception
    {
        Integer inc = null;
        SeriesNode series = null;
        
        //find our #
        for(ASTNode child : children)
        	if(child instanceof ParameterNode && inc == null)
        		inc = Integer.parseInt(child.getData().getTokenString());
        	else if(child instanceof SeriesNode && series == null)
        		series = (SeriesNode) child;
        
        if(series != null)
        	for(int i = 0; i<inc; i++)
        		series.run(input);
    }
}